<?
set_time_limit(1000);
require_once("../config/dbconn.php");require_once("../config/usercls.php");

$sqlhy="select * from {$db_prefix}users where username='".$_SESSION["sys_username"]."'";
$rshy=$db->get_one($sqlhy);

if($action=='buy'){
	$hint='';
	if(intval($num)!=floatval($num)) $hint.="购买数量不是整数\\n";
	if($hint!=''){
		echo "<script>alert('$hint');history.back();</script>";exit();
	}
	
	//会员账户中是否有足够的电子股
	$curtradeprice=intval($num)*$glo_gpcurprice;
	if ($rshy['gpprice']<$curtradeprice){
		$hint="电子股的余额不足。本次交易需要{$curtradeprice}美金";
		echo "<script>alert('$hint');history.back();</script>";exit();
	}
	
	//查看原始股数量是否足够
	if ($glo_gpysgnum<intval($num)){
		$hint="原始股数量不足{$num}";
		echo "<script>alert('$hint');history.back();</script>";exit();
	}
	
	//扣除会员的电子股金额
	$sqlq="update {$db_prefix}users set gpnum=gpnum+'".intval($num)."',gpprice=gpprice-'$curtradeprice' where username='".$_SESSION["sys_username"]."'";
	$db->query($sqlq);
	//插入到电子政务记录中 注册人的电子政务
	$e_userid=$_SESSION["sys_userid"];$e_price=-$curtradeprice;$e_type=1;$e_ptype=4;$e_addtime=$curtime;$e_memo="买入原始报单币";
	hyepricejl($e_userid,$e_price,$e_type,$e_ptype,$e_addtime,$e_memo);
	
	//插入原始股购买记录
	$sqljl="insert into {$db_prefix}gp_tradeysg(username,num,price,addtime) values('".$_SESSION["sys_username"]."','".intval($num)."','".$rsgp['price']."','".$modtime."')";
	$db->query($sqljl);
	
	//更新原始股的数量
	$sqlsys="update {$db_prefix}gupiaoset set gpysgnum=gpysgnum-'".intval($num)."',gpsaleoutnum=gpsaleoutnum+'".intval($num)."' where 1";
	$db->query($sqlsys);
	
	//看报单币已经销售了多少了
	$curgp_synum=$glo_gpsaleoutnum+intval($num);
	if ($curgp_synum>=($glo_gp_scgupiaonum)){
		$curgp_szprice=$glo_gp_kpprice+floor($curgp_synum/($glo_gp_scgupiaonum))*0.0001;
		if ($glo_gpcurprice<$curgp_szprice){
			//更新报单币价格
			$sqlsys="update {$db_prefix}gupiaoset set gpcurprice='$curgp_szprice' where 1";
			$db->query($sqlsys);
			//报单币每涨0.0499美金系统自动挂牌卖出70%。卖出70%的其中30%用于当前价格回购
			///////////////////////////////////////////////////////
			$sql="select * from {$db_prefix}users where state=1";
			$result=$db->query($sql);
			while($rs=$db->fetch_array($result)){
				$curtradenum=$rs['gpnum']*$glo_gpsalerate/100;
				$curtradenumbuy=$curtradenum*$glo_gpbuyrate/100;
				$curtradenum=$curtradenum-$curtradenumbuy;
				//扣除会员的原始股量
				$sqlq="update {$db_prefix}users set gpnum=gpnum-'$curtradenum' where username='".$rs['username']."'";
				$db->query($sqlq);
				//插入交易记录中
				$sql="insert into {$db_prefix}gp_market(username,num,num1,price,jztime,type,addtime,state) values('".$rs['username']."','".$curtradenum."','".$curtradenum."','".$curgp_szprice."','".($curtime+24*3600*30)."','2','".$curtime."',0)";
				$db->query($sql);
				$curmarketid=$db->insert_id();
				
				$price=$curgp_szprice;
				$num=$curtradenum;
				
				require("gptrading2.php");
			}
			$db->free_result($result);			
			////////////////////////////////////////////////////////
		}
	}
	echo "<script>alert('购买成功');location.href='gp_buy.php';</script>";exit();
}
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<style type="text/css">
<!--
body {
	margin-left: 0px;
	margin-top: 0px;
	margin-right: 0px;
	margin-bottom: 0px;
	
}
-->
</style>
<script language="javascript" type="text/javascript" src="../calendar/WdatePicker.js"></script>
<link href="images/skin.css" rel="stylesheet" type="text/css" />
</head>
<body>
<table width="100%" border="0" cellpadding="0" cellspacing="0">
  <tr>
    <td width="17" height="29" valign="top" background="images/mail_leftbg.gif"><img src="images/left-top-right.gif" width="17" height="29" /></td>
    <td width="935" height="29" valign="top" background="images/content-bg.gif"><table width="100%" height="31" border="0" cellpadding="0" cellspacing="0" class="left_topbg" id="table2">
      <tr>
        <td height="31"><div class="titlebt"><strong>原始股购买</strong></div></td>
      </tr>
    </table></td>
    <td width="16" valign="top" background="images/mail_rightbg.gif"><img src="images/nav-right-bg.gif" width="16" height="29" /></td>
  </tr>
  <tr>
    <td height="71" valign="middle" background="images/mail_leftbg.gif">&nbsp;</td>
    <td valign="top" bgcolor="#F7F8F9"><table width="100%" height="138" border="0" cellpadding="0" cellspacing="0">
      <tr>
        <td height="13" valign="top">&nbsp;</td>
      </tr>
      <tr>
        <td valign="top">
		
		<table width="100%" border="0" cellspacing="0" cellpadding="0">
            <tr>
              <td width="100%"><table width="100%" height="31" border="0" cellpadding="0" cellspacing="0" class="nowtable">
                <tr>
                  <td class="left_bt2">&nbsp;&nbsp;&nbsp;&nbsp;</td>
                </tr>
              </table></td>
            </tr>
            <tr>
              <td height="30"><form name="form1" method="post" action="?action=buy">
                <table width="100%" height="120" border="0" cellpadding="0" cellspacing="0">
                  <tr>
                    <td height="30" align="right" bgcolor="#FFFFFF" class="left_txt">购买数量：</td>
                    <td align="left" bgcolor="#FFFFFF" class="left_txt"><input name="num" type="text" id="num" size="10" value="<?=$glo_gp_lowertrade?>"></td>
                  </tr>
                  <tr>
                    <td height="30" align="right" bgcolor="#FFFFFF" class="left_txt">报单币当前价格：</td>
                    <td align="left" bgcolor="#FFFFFF" class="left_txt"><?=$glo_gpcurprice?></td>
                  </tr>
                  <tr>
                    <td height="30" align="right" bgcolor="#FFFFFF" class="left_txt">报单币金额：</td>
                    <td align="left" bgcolor="#FFFFFF" class="left_txt"><?=$rshy['gpprice']?></td>
                  </tr>
                  <tr>
                    <td height="30" align="right" bgcolor="#FFFFFF" class="left_txt">&nbsp;</td>
                    <td align="left" bgcolor="#FFFFFF" class="left_txt"><input type="submit" name="Submit" value="购买"></td>
                  </tr>
                </table>
                            </form>
              </td>
            </tr>
            <tr>
              <td height="30">&nbsp;</td>
            </tr>
          </table>
		 
		  <p>&nbsp;</p></td>
      </tr>
    </table></td>
    <td background="images/mail_rightbg.gif">&nbsp;</td>
  </tr>
  <tr>
    <td valign="middle" background="images/mail_leftbg.gif"><img src="images/buttom_left2.gif" width="17" height="17" /></td>
      <td height="17" valign="top" background="images/buttom_bgs.gif"><img src="images/buttom_bgs.gif" width="17" height="17" /></td>
    <td background="images/mail_rightbg.gif"><img src="images/buttom_right2.gif" width="16" height="17" /></td>
  </tr>
</table>

</body>
</html>